from django.shortcuts import render_to_response

# connect to RPC server to talk with workflow server
import xmlrpclib
server = xmlrpclib.Server('http://localhost:8001')
#def say(request, name):#    return render_to_response('index.html', {'name': name})      
def say(request):    return render_to_response('index.html')    


#from django import template#from django.template.defaultfilters import stringfilter#register = template.Library()
#
#@register.filter
#def type_filter(value):
#    return "hi"
#    #return str(type(value))

# this "decorator" enables form values to be submitted (otherwise Django errors out on submission)
from django.views.decorators.csrf import csrf_exempt@csrf_exempt
def processing_workflow(request):
    #return render_to_response('processing_workflow.html', {'name': 'fooval'})  
    step_legend = ['1. Receive glass from manufacturers',
                   '2. Specify recipe',
                   '3. SRD (Spin/Rinse Dryer) load and unload',
                   '4. H2O DI Resistivity',
                   '5. Q: is it clean']
    output_dict = {'field1':'val1', 
                   'field2':'val2'}
    input_dict = {'recv_date':'val1', 
                  'manuf_date':'val2',
                  'unwrap_date':'val2',
                  'vendor':'val2'}
   
#    if 'next_step' not in request.GET:
#        next_step = 1
#    else:
#        next_step = int(request.GET['next_step'])+1 
        
    # I/O for each step/actor
#    if next_step == 1:    
#	    output_dict = {'no_context':'this step has no information to display to the user'}
#	    input_dict = {'recv_date':'val1', 
#	                  'manuf_date':'val2',
#	                  'unwrap_date':'val2',
#	                  'vendor':'val2'}
#    elif next_step == 2:    
#	    output_dict = {'recv_date':request.GET['recv_date'], 
#	                  'manuf_date':request.GET['manuf_date'],
#	                  'unwrap_date':request.GET['unwrap_date'],
#	                  'vendor':request.GET['vendor']}
#	    input_dict = {'recipe':'H2SO4', }
#	    
#    elif next_step == 3:    
#	    output_dict = {'recv_date':request.GET['recv_date'], 
#	                  'manuf_date':request.GET['manuf_date'],
#	                  'unwrap_date':request.GET['unwrap_date'],
#	                  'vendor':request.GET['vendor']}
#	    input_dict = {'recipe':'H2SO4', }
	               
    import pprint
    print "#####"
    pprint.pprint(dict(request.GET))
    
    
    
    
    #r = server.fd_step('processing_workflow.txt', dict(request.GET))
    r = server.execute_until_input_is_needed('processing_workflow.txt', dict(request.GET))
    print pprint.pformat(r['r'])
    
    # variables to send HTML template
    if 'next_step' in r['r']:
        next_step = r['r']['next_step']
    else:
        next_step = 1
    return render_to_response('processing_workflow.html', {'next_step':next_step,
    	                                                   'step_legend':r['r']['step_legend'], 
                                                           'output_dict':r['r']['output_dict'],
                                                           'input_dict':r['r']['input_dict']})
    
def reliability_testing_workflow(request):
    return render_to_response('reliability_testing_workflow.html', {'name': 'fooval'})   

def data_analysis_workflow(request):
    return render_to_response('data_analysis_workflow.html', {'name': 'fooval'})   